﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SystemCenterSoa.ApiLibrary.PermissionsValidation;
using SystemCenterSoa.ApiProject.Controllers;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.DtoModel.SystemManager;
using SystemCenterSoa.PoModel.SystemManager;
using SystemCenterSoa.Service.SystemManager;

namespace SystemCenterSoa.ApiProject.Area.SystemManager
{
    [ApiModuleRelation("role_manager")]
    public class RoleManagerController : MyBaseController
    {
        private RoleService _roleDbBll;
        public RoleManagerController(RoleService roleDbBll)
        {
            _roleDbBll = roleDbBll;
        }
        [HttpGet]
        public ResultModel<RoleDbViewDto> GetRoleViewById(int roleId)
        {
            try
            {
                var data = _roleDbBll.GetRoleViewById(roleId);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<RoleDbViewDto>(ex);
            }
        }
        [HttpPost]
        public ResultModel<PagerResult<RolePo>> SearchPage(SearchModel sm)
        {
            try
            {
                var data = _roleDbBll.SearchPage(sm);
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<PagerResult<RolePo>>(ex);
            }


        }

        [HttpPost]
        public ResultModel<int> RoleDbAdd(RoleDbViewDto view)
        {
            try
            {
                var data = _roleDbBll.RoleDbAdd(view);
                if (data == 0) //返回添加id为0,  说明添加没有成功, 直接返回失败
                {
                    return MyFail<int>("添加没有成功!");
                }
                return MyOk(data);
            }
            catch (Exception ex)
            {
                return MyError<int>(ex);
            }
        }
        [HttpPost]
        public ResultModel<bool> RolePoUpdate(RoleDbViewDto view)
        {
            try
            {
                var data = _roleDbBll.RoleUpdate(view);
                if (data)
                {
                    return MyOk(data);
                }
                return MyFail<bool>("更新没有成功！");
            }
            catch (Exception ex)
            {
                return MyError<bool>(ex);
            }
        }
    }
}
